USE [NGroup_VTL_DB]
GO

/****** Object:  StoredProcedure [dbo].[CheckUniqueStringGT]    Script Date: 01/12/2014 03:38:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CheckUniqueStringGT]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CheckUniqueStringGT]
GO

USE [NGroup_VTL_DB]
GO

/****** Object:  StoredProcedure [dbo].[CheckUniqueStringGT]    Script Date: 01/12/2014 03:38:56 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		Nguyen Vu Nam
-- Create date: 20140111
-- Description:	Check if unique string is existing in generic types
-- =============================================
CREATE PROCEDURE [dbo].[CheckUniqueStringGT]
	@SupTypeID		INT = NULL,
	@TypeID			INT = NULL,
	@UniqueString	NVARCHAR(500),
	@IsExisting		BIT OUTPUT
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	IF(@SupTypeID IS NULL)
	BEGIN
		IF NOT EXISTS(
						SELECT * 
						FROM [dbo].[GenericTypes] WITH (NOLOCK) 
						WHERE [UniqueString] = @UniqueString
						AND ([ID] <> @TypeID OR [ID] IS NOT NULL)
						AND  SupTypeID IS NULL
						AND [IsActive] <> 0)
		BEGIN
			SELECT @IsExisting = 0
		END	
		ELSE
		BEGIN
			SELECT @IsExisting = 1
		END
	END
	ELSE
	BEGIN
		IF NOT EXISTS(
						SELECT * 
						FROM [dbo].[GenericTypes] WITH (NOLOCK) 
						WHERE [UniqueString] = @UniqueString
						AND ([ID] <> @TypeID OR [ID] IS NOT NULL)
						AND SupTypeID = @SupTypeID
						AND [IsActive] <> 0)
		BEGIN
			SELECT @IsExisting = 0
		END	
		ELSE
		BEGIN
			SELECT @IsExisting = 1
		END
	END

    
END







GO

